package com.dayanxs.Controller;


import com.dayanxs.Service.NovelService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/novel")
public class NovelController {

    private final NovelService novelService;

    public NovelController(NovelService novelService) {
        this.novelService = novelService;
    }

    @PostMapping("/add")
    public ResponseEntity<?> addNovel(
            @RequestHeader(value = "Authorization", required = false) String authorization,
            @RequestBody Map<String, String> novelDetails) {
        return novelService.addNovel(authorization, novelDetails);
    }

    @GetMapping("/random")
    public ResponseEntity<?> getRandomNovels() {
        return novelService.getRandomNovels();
    }

    @GetMapping("/search")
    public ResponseEntity<?> searchNovels(@RequestParam String novelName) {
        return novelService.searchNovels(novelName);
    }
    @GetMapping("/all")
    public ResponseEntity<?> getNovelsWithPagination(
            @RequestParam(defaultValue = "10") int size, // 默认每页 10 条
            @RequestParam(defaultValue = "1") int page) {
        return novelService.getNovelsWithPagination(size, page); // 逻辑处理在 Service 层
    }
    @GetMapping("/delete")
    public ResponseEntity<?> deleteNovel(
            @RequestParam int novelId,
            @RequestHeader(value = "Authorization", required = false) String authorization) {
        return novelService.deleteNovel(novelId, authorization);
    }

    @PostMapping("/update")
    public ResponseEntity<?> updateNovel(
            @RequestParam int novelId,
            @RequestHeader(value = "Authorization", required = false) String authorization,
            @RequestBody Map<String, String> novelDetails) {
        return novelService.updateNovel(novelId, authorization, novelDetails);
    }
    @GetMapping("/info")
    public ResponseEntity<?> getNovelById(@RequestParam int novelId) {
        return novelService.getNovelById(novelId);
    }
    @PostMapping("/addnac")
    public ResponseEntity<?> addNovelAndChapter(@RequestBody Map<String, String> data) {
        return novelService.addNovelAndChapter(data);
    }

}

